package com.newtype.meeting.client.widget.shared;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class PersonnelSingleSelectWindow extends Window {

	private TreeStore<ModelData> store = new TreeStore<ModelData>();

	public PersonnelSingleSelectWindow() {
		setSize(450, 500);
		setModal(true);
		setLayout(new BorderLayout());

		createWest();
		createCenter();

		addButton(getConfirmButton());
		addButton(getCancelButton());
	}

	/**
	 * 取消按钮
	 * @return
	 */
	private Button getCancelButton() {
		Button button = new Button("取消");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				hide();
			}
		});
		return button;
	}

	/**
	 * 确定选取按钮
	 * @return
	 */
	private Button getConfirmButton() {
		Button button = new Button("确定选取");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {

			}
		});
		return button;
	}

	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				225, 200, 250);
		westData.setMargins(new Margins(5, 0, 5, 5));
		VerticalPanel westPanel = new VerticalPanel();
		westPanel.setSpacing(8);
		westPanel.setBorders(true);
		westPanel.setLayout(new FormLayout());
		westPanel.setScrollMode(Scroll.AUTO);

		final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
		tree.setBorders(false);
		tree.setDisplayProperty("name");
		tree.setCheckable(true);
		tree.setAutoLoad(true);
		tree.setIconProvider(new TreeModelIconProvider());		
		tree.setCheckStyle(CheckCascade.CHILDREN);
		tree.setCheckNodes(CheckNodes.LEAF);  
		
		ButtonBar buttonBar = new ButtonBar();
		buttonBar.add(new Button("Expand All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.expandAll();
					}
				}));
		buttonBar.add(new Button("Collapse All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.collapseAll();
					}
				}));
		westPanel.add(buttonBar);
		westPanel.add(tree);

		add(westPanel, westData);
	}

	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeading("已选部门");

		add(centerPanel, centerData);
	}

	public TreeStore<ModelData> getStore() {
		return store;
	}
	
	/**
	 * 图标显示
	 * @author 汪恭泽
	 *
	 */
	public class TreeModelIconProvider implements ModelIconProvider<ModelData> {
		@Override
		public AbstractImagePrototype getIcon(ModelData model) {
			if(model.get("accountID")!=null){
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/member.gif");
			}else{
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/dept.gif");
			}
		}
	}
}
